<?php

namespace App\Http\Controllers\Estate;

use App\Http\Controllers\Controller;
use App\Http\Resources\Estate\UserResource\UserCollection;
use App\Models\AdminEstate;
use App\Models\User;
use App\Services\UserService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, User $user_model)
    {
        $list = $user_model->orderBy('id', 'desc')->paginate($request->per_page ?? 30);
        return $this->success(new UserCollection($list));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, User $user_model)
    {
        if ($user_model->where('username', $request->username)->exists()) {
            return $this->error(__('admins.username_existence'));
        }
        if ($user_model->where('phone', $request->phone)->exists()) {
            return $this->error(__('admins.phone_existence'));
        }
        $user_model = $user_model->create([
            'username' => $request->username,
            'phone'    => $request->phone,
            'password' => Hash::make($request->password ?? '123456'),
            'nickname' => $request->nickname ?? '神秘人',
            'avatar'   => $request->avatar ?? '',
        ]);

        return $this->success([], __('base.success'));
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show(User $user_model, $id)
    {
        $info = $user_model->find($id);
        return $this->success($info);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, User $user_model, $id)
    {
        if ($user_model->where('username', $request->username)
            ->where('id', '<>', $id)
            ->exists()
        ) {
            return $this->error(__('admins.username_existence'));
        }
        if ($user_model->where('phone', $request->phone)
            ->where('id', '<>', $id)
            ->exists()
        ) {
            return $this->error(__('admins.phone_existence'));
        }
        $user_model           = $user_model->find($id);
        $user_model->username = $request->username;
        if (!empty($request->password)) {
            $user_model->password = Hash::make($request->password ?? '123456');
        }
        $user_model->nickname = $request->nickname ?? '神秘人';
        $user_model->avatar   = $request->avatar ?? '';
        $user_model->save();
        return $this->success([], __('base.success'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(User $user_model, $id)
    {
        $idArray = array_filter(explode(',', $id), function ($item) {
            return is_numeric($item);
        });

        $user_model->destroy($idArray);
        return $this->success([], __('base.success'));
    }

    /**
     * Notes: 修改用户信息
     * User: lstink
     * Date: 2021/1/29
     * Time: 6:05 下午
     * @param Request $request
     * @param AdminEstate $admin_estate
     */
    public function changeInfo(Request $request, UserService $userService)
    {
        $username = $request->post('username', '');
        $password = $request->post('password', '');
        //获取用户信息
        $user_info = $request->user();
        if (!empty($username)) {
            $user_info->username = $username;
        }
        if (!empty($password)) {
            $user_info->password = Hash::make($password);
        }
        $user_info->save();
        //获取用户信息
        $user_info = $userService->getUserInfo('estate');
        return $this->success($user_info, __('base.success'));
    }
}
